﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class administrator_ChangeInfomationUser : System.Web.UI.Page
{
    dulieuDataContext db=new dulieuDataContext();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            LoadUser();
        }
    }
    protected void LoadUser()
    {
        rptmnu.DataSource = from o in db.Users select o;
        rptmnu.DataBind();
    }
    protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
    {
        var user=new User(){UserName = txtmenu.Text,Pass = txtmatkhau.Text,Role = false};
        db.Users.InsertOnSubmit(user);
        db.SubmitChanges();
        lblthongbao.Text = "Thêm thành công";
        System.Threading.Thread.Sleep(800);
        LoadUser();
    }
    protected void UpdateCheck(object s, RepeaterCommandEventArgs e)
    {
        if (e.CommandName == "edit")
        {
            var lblid = (Label)e.Item.FindControl("lblid");
            Session.Add("ID", lblid.Text);
            var user = (from o in db.Users where o.ID == Convert.ToInt32(lblid.Text) select o).SingleOrDefault();
            if (user != null)
            {
                txtmenu.Text =user.UserName;
                Session.Add("UserName",user.UserName);
                if (true) txtmatkhau.Text = user.Pass;
            }
            System.Threading.Thread.Sleep(800);
           LoadUser();
            txtmenu.Enabled = false;
        }
    }
    protected void ImageButton2_Click(object sender, ImageClickEventArgs e)
    {
        var user = (from o in db.Users where o.ID == Convert.ToInt32(Session["ID"]) select o).SingleOrDefault();
        if (user != null) user.Pass = txtmatkhau.Text;
        db.SubmitChanges();
        lblthongbao.Text = "Cập nhật thành công";
        txtmenu.Enabled = true;

    }
    protected void ImageButton3_Click(object sender, ImageClickEventArgs e)
    {
        foreach (RepeaterItem item in rptmnu.Items)
        {
            var chk = (CheckBox) item.FindControl("chk");
            if (chk.Checked)
            {
                var lblid = (Label) item.FindControl("lblid");
                var user =(from o in db.Users where o.ID == Convert.ToInt32(lblid.Text) select o).FirstOrDefault();
                if (user != null) db.Users.DeleteOnSubmit(user);
                db.SubmitChanges();
            }
        }
        System.Threading.Thread.Sleep(800);
        LoadUser();
        lblthongbao.Text = "Xóa thành công";
    }
}